use SAS
GO

 IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'InsUpdAccountReseller')
	BEGIN
		DROP  Procedure  dbo.InsUpdAccountReseller
	END
GO

CREATE Procedure [dbo].[InsUpdAccountReseller] (
	@ACCOUNTNO				VARCHAR(10)
,	@ACCOUNTGUID			VARCHAR(100)	
,	@RESELLERID				VARCHAR(10)	 = NULL
,	@OTHERRESELLER			VARCHAR(100) = NULL
)
AS
BEGIN

	IF( CAST(ISNULL(@RESELLERID,'0') AS INT) = 0 AND ISNULL(@OTHERRESELLER,'') = '')
	BEGIN
		DELETE FROM TR_ACCOUNT
		WHERE CAST(ACCOUNTGUID AS VARCHAR(100)) = @ACCOUNTGUID 
	END
	ELSE IF EXISTS(SELECT 1 FROM TR_ACCOUNT WHERE CAST(ACCOUNTGUID AS VARCHAR(100)) = @ACCOUNTGUID )
	BEGIN
		UPDATE TR_ACCOUNT
		SET ResellerNo = NULLIF(@RESELLERID,0)
		,	OtherReseller = NULLIF(@OTHERRESELLER,'')
		WHERE
			CAST(ACCOUNTGUID AS VARCHAR(100)) = @ACCOUNTGUID
	END
	ELSE
	BEGIN
		INSERT INTO DBO.TR_ACCOUNT (AccountNo, AccountGUID, ResellerNo, OtherReseller)
		VALUES(@ACCOUNTNO, @ACCOUNTGUID, NULLIF(@RESELLERID,0), NULLIF(@OTHERRESELLER,''))
	END
END
GO
